package main;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.game.Sprite;
import javax.microedition.lcdui.Image;
import javax.microedition.m3g.Graphics3D;
import javax.wireless.messaging.BinaryMessage;

public class Pokemon {

    public static final int STAND = 0;
    public static final int MOVELEFT = 1;
    public static final int MOVERIGHT = 2;
    public static final int FIRE = 3;
    public static final int BE_HIT = 4;
    private String _name;
    private int _speed;
    private int _attack;
    //private int _defend;
    private int _maxHp;
    private int _currentHp;
    private int _hp;
    private double _angle;
    private int _status;
    private PokemonSprite _pokemonSprite;
    private ThrewObject throwingObject;
    private int _hpColor = 0x00FF0000;

    public boolean healing(int d)
    {
        if(_currentHp == _maxHp)
            return true;
        _currentHp += d;
        return false;
    }
    
    public void drawInfo(Graphics g, int x, int y)
    {
        g.drawString("Name: " + _name, x, y, Graphics.BASELINE|Graphics.LEFT);
        g.drawString("Speed: " + String.valueOf(_speed), x, y + 15, Graphics.BASELINE|Graphics.LEFT);
        g.drawString("Attack: " + String.valueOf(_attack), x, y + 30, Graphics.BASELINE|Graphics.LEFT);
        g.drawString("MaxHP: " + String.valueOf(_maxHp), x, y + 45, Graphics.BASELINE|Graphics.LEFT);
        g.drawString("HP: " + String.valueOf(_currentHp), x, y + 60, Graphics.BASELINE|Graphics.LEFT);
    }
    public void setColor(int c) {
        _hpColor = c;
    }

    private void init() {
        _hp = 1000;
        _attack = 100;
        _maxHp = 1000;
        _currentHp = _hp - 400;
        //_defend = 1000;
    }

    public int getAttack() {
        return _attack;
    }

    public int getSpeed() {
        return _speed;
    }

    public int getHp() {
        return _hp;
    }

    public int getMaxHp() {
        return _maxHp;
    }

    public void incHp(int dHp) {
        _hp += dHp;
        if (_hp > _maxHp) {
            _hp = _maxHp;
        } else if (_hp < 0) {
            _hp = 0;
        }
    }

    public double getAngle() {
        return _angle;
    }

    public boolean alterHp() {

        if (_hp < _currentHp) {
            _currentHp -= 10;
            if (_hp > _currentHp) {
                _currentHp = _hp;
            }

        } else if (_hp > _currentHp) {
            _currentHp += 10;
            if (_hp < _currentHp) {
                _currentHp = _hp;
            }
        }
        if (_hp == _currentHp) {
            return true;
        }
        return false;
    }

    public Pokemon(String name, String fileName, int w, int h, int stand, int move, int hit, int fire) {
        init();
        _name = name;
        _status = STAND;
        // = JUMP;
        _pokemonSprite = new PokemonSprite(fileName, w, h, stand, move, hit, fire);
        _angle = Math.PI;
        throwingObject = new ThrewObject();
        throwingObject.setThrowParameter(0, 0);
        throwingObject.activate();
    }

    public Sprite getSprite() {
        return _pokemonSprite.sprite;
    }

    private void incAngle(double d) {
        int m = (int) (d / 2 * Math.PI);
        d = d - m * 2 * Math.PI;
        _angle += d;
        if (_angle >= 2 * Math.PI) {
            _angle = _angle - 2 * Math.PI;
        } else if (_angle < 0) {
            _angle = _angle + 2 * Math.PI;
        }
    }

    private void invertAngle() {
        _angle = Math.PI - _angle;
        if (_angle < 0) {
            _angle = 2 * Math.PI + _angle;
        }
    }

    public void gunUp() {
        double a = 0.15;
        if (_angle > Math.PI / 2 && _angle < 3 * Math.PI / 2) {
            if (_angle > (Math.PI / 2 + a)) {
                incAngle(-a);
            }
        } else {
            if (_angle < (Math.PI / 2 - a) || _angle >= 3 * Math.PI / 2) {
                incAngle(a);
            }
        }

    }

    public void gunDown() {
        double a = 0.15;
        if (_angle > Math.PI / 2 && _angle < 3 * Math.PI / 2) {
            if (_angle < (3 * Math.PI / 2 - a)) {
                incAngle(a);
            }
        } else {
            if (_angle > (3 * Math.PI / 2 + a) || _angle <= Math.PI / 2) {
                incAngle(-a);
            }
        }

    }

    public void Move(boolean isMoveLeft, Sprite map) {

        if (isMoveLeft) {
            if (_status != MOVELEFT) {
                _pokemonSprite.sprite.setFrameSequence(_pokemonSprite.PokemonSeqMove);
                _pokemonSprite.sprite.setTransform(Sprite.TRANS_NONE);
                _status = MOVELEFT;
                if (_angle < Math.PI / 2 || _angle > 3 * Math.PI / 2) {
                    invertAngle();
                }
            } else {

                int count = 0;
                for (int i = 0; i < 3; i++) {
                    count += moveSpriteOnGround(_pokemonSprite.sprite, map, -1, 3);
                    if (count > 3) {
                        break;
                    }
                }

            }
        } else {
            if (_status != MOVERIGHT) {
                _pokemonSprite.sprite.setFrameSequence(_pokemonSprite.PokemonSeqMove);
                _pokemonSprite.sprite.setTransform(Sprite.TRANS_MIRROR);
                _status = MOVERIGHT;
                if (_angle > Math.PI / 2 && _angle < 3 * Math.PI / 2) {
                    invertAngle();
                }
            } else {

                int count = 0;
                for (int i = 0; i < 3; i++) {
                    count += moveSpriteOnGround(_pokemonSprite.sprite, map, 1, 3);
                    if (count > 3) {
                        break;
                    }
                }
            }
        }
    }

    public void Stand() {
        if (_status != STAND) {
            _pokemonSprite.sprite.setFrameSequence(_pokemonSprite.PokemonSeqStand);
            _status = STAND;
        }
    }

    public void Fire() {
        if (_status != FIRE) {
            _pokemonSprite.sprite.setFrameSequence(_pokemonSprite.PokemonSeqFire);
            _status = FIRE;
        }
    }

    public void beHit() {
        if (_status != BE_HIT) {
            _pokemonSprite.sprite.setFrameSequence(_pokemonSprite.PokemonSeqHit);
            _status = BE_HIT;
        }
    }

    public void run(Sprite map) {
        if (_pokemonSprite.sprite.collidesWith(map, true)) {
            throwingObject.reset();
            if (_status != BE_HIT || _pokemonSprite.sprite.getFrame() != _pokemonSprite.sprite.getFrameSequenceLength()-1) {
                _pokemonSprite.sprite.nextFrame();
            }


        } else {
            int x = _pokemonSprite.sprite.getRefPixelX();
            int y = _pokemonSprite.sprite.getRefPixelY();
            Point positionPoint = new Point(x, y);
            throwingObject.run(positionPoint);
            ThrewObject.moveSpriteTo(_pokemonSprite.sprite, map, positionPoint.getX(), positionPoint.getY(), true);
        }

    }

    public void paint(Graphics g, int dx, int dy, boolean drawHpBar, boolean drawAngle) {


        g.setColor(_hpColor);

        if (drawAngle && _status == STAND) {
            int x = _pokemonSprite.sprite.getX() + _pokemonSprite.sprite.getWidth() / 2 + dx;
            int y = _pokemonSprite.sprite.getY() + _pokemonSprite.sprite.getHeight() / 2 + dy;
            int x1 = x + (int) (30 * Math.cos(_angle));
            int y1 = y - (int) (30 * Math.sin(_angle));
            g.drawLine(x, y, x1, y1);
        }
        if (drawHpBar) {
            g.fillRect(_pokemonSprite.sprite.getX() + dx, _pokemonSprite.sprite.getY() + dy, _currentHp * _pokemonSprite.sprite.getWidth() / _maxHp, 3);
            g.setColor(0x00FFFFFF);
            g.drawRect(_pokemonSprite.sprite.getX() + dx, _pokemonSprite.sprite.getY() + dy, _pokemonSprite.sprite.getWidth(), 3);
        }
        _pokemonSprite.sprite.move(dx, dy);
        _pokemonSprite.sprite.paint(g);
        _pokemonSprite.sprite.move(-dx, -dy);
//g.drawString(String.valueOf(_pokemonSprite.sprite.getFrame()), 100, 100, 0);
    }

    private int moveSpriteOnGround(Sprite sp, Sprite map, int dx, int maxLoop) {
        int count = -1;
        Sprite pos = new Sprite(Image.createImage(1, 1));
        pos.defineReferencePixel(sp.getX() - sp.getRefPixelX(), sp.getY() - sp.getRefPixelY());

        int x = sp.getRefPixelX();
        int y = sp.getRefPixelY();
        pos.setPosition(x + dx, y + 1);

        while (pos.collidesWith(map, true) && count <= maxLoop) {
            pos.move(0, -1);
            count++;
        }
        if (count >= 0) {
            pos.move(0, 1);
        }
        if (count <= maxLoop) {
            sp.setPosition(pos.getRefPixelX(), pos.getRefPixelY());
        }

        return count;
    }

    public static void moveUpTheGround(Sprite sp, Sprite map, int maxLoop) {
        int count = 0;
        Sprite pos = new Sprite(Image.createImage(1, 1));
        pos.defineReferencePixel(sp.getX() - sp.getRefPixelX(), sp.getY() - sp.getRefPixelY());

        int x = sp.getRefPixelX();
        int y = sp.getRefPixelY();
        pos.setPosition(x, y);
        while (pos.collidesWith(map, true) && count <= maxLoop) {
            pos.move(0, -1);
            count++;
        }
        if (count > 0) {
            pos.move(0, 1);
            sp.setPosition(pos.getRefPixelX(), pos.getRefPixelY());
        }
    }

    private class PokemonSprite {

        public Sprite sprite;
        public int[] PokemonSeqStand;
        public int[] PokemonSeqMove;
        public int[] PokemonSeqHit;
        public int[] PokemonSeqFire;

        public PokemonSprite(String fileName, int w, int h, int stand, int move, int hit, int fire) {

            int i = 0;
            int index = 0;

            PokemonSeqStand = new int[stand];
            for (i = 0; i < stand; i++) {
                PokemonSeqStand[i] = index++;
            }

            PokemonSeqMove = new int[move];
            for (i = 0; i < move; i++) {
                PokemonSeqMove[i] = index++;
            }
            PokemonSeqHit = new int[hit];
            for (i = 0; i < hit; i++) {
                PokemonSeqHit[i] = index++;
            }

            PokemonSeqFire = new int[fire];
            for (i = 0; i < fire; i++) {
                PokemonSeqFire[i] = index++;
            }

            try {
                sprite = new Sprite(Image.createImage(fileName), w, h);
                sprite.defineReferencePixel(w / 2, h / 2);
                sprite.setFrameSequence(PokemonSeqStand);
            } catch (Exception ex) {
            }
            if (stand <= 0 || move <= 0 || hit <= 0 || fire <= 0) {
                return;
            }
        }
    }
}
